package cn.cool.config;

import cn.cool.task.FollowTask;
import cn.cool.task.LikeTask;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LiuH
 * @create 2021-02-08 下午 3:21
 */
@Configuration
public class MyQuartzConfig {

    private static final String LIKE_TASK_IDENTITY = "LikeTaskQuartz";

    private static final String FOLLOW_TASK_IDENTITY = "FollowTaskQuartz";

    //点赞持久化任务
    @Bean
    public JobDetail quartzDetail(){
        return JobBuilder.newJob(LikeTask.class).withIdentity(LIKE_TASK_IDENTITY).storeDurably().build();
    }

    @Bean
    public Trigger quartzTrigger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInHours(2) //两个小时执行一次
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(quartzDetail())
                .withIdentity(LIKE_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }



    //用户关注持久化任务
    @Bean
    public JobDetail quartzDetail1(){
        return JobBuilder.newJob(FollowTask.class).withIdentity(FOLLOW_TASK_IDENTITY).storeDurably().build();
    }

    @Bean
    public Trigger quartzTrigger1(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInHours(1) //一个小时执行一次
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(quartzDetail1())
                .withIdentity(FOLLOW_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }
}
